package util;

import bean.DataPoint;
import bean.DelteData;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 该类将从源数据中筛选出连续增加或连续减少的点
 * @author :chensi
 */
public class SevenPointFunction {

    public List<DelteData[]> sevenPointFunction(DataPoint[] sourceData, double midValue, Integer continueNum){
        List<DelteData[]> result = sevenPointFunction(sourceData, continueNum);
        //过滤结果集
        if(result.size()==0){
            return result;
        }
        Iterator<DelteData[]> iter = result.iterator();
        while (iter.hasNext()){
            DelteData[] delteData = iter.next();
            double firstDifValue = delteData[0].getPreMetricData().getValue() - midValue;
            boolean remove = true;
            for(int i=0;i<delteData.length;i++){
                double plus = firstDifValue * (delteData[i].getSufMetricData().getValue() - midValue);
                if(plus<=0){
                    remove = false;
                    break;
                }
            }
            if(remove) {
                iter.remove();
            }
        }
        return result;
    }

    public List<DelteData[]> sevenPointFunction(DataPoint[] sourceData, Integer continueNum) {
        if(continueNum==null){
            continueNum = 6;
        }
        //处理源数据生成delte数组
        DelteData[] delteArray = new DelteData[sourceData.length-1];
        for(int i=0;i<sourceData.length-1;i++){
            delteArray[i] = new DelteData(sourceData[i],sourceData[i+1],sourceData[i+1].getValue()-sourceData[i].getValue());
        }
        int last = 0;
        boolean first = true;
        List<DelteData> tmpL = new ArrayList<DelteData>();
        List<DelteData[]> result = new ArrayList<DelteData[]>();
        for (int i = 0; i < delteArray.length; i++) {
            //差值为正数
            if (delteArray[i].getDelteValue() > 0) {
                last = process(first, 1, i, last, continueNum, delteArray, tmpL, result);
                first = false;
            }
            //差值为0
            else if (delteArray[i].getDelteValue() == 0) {
                addToResult(tmpL, continueNum, result);
                tmpL.clear();
                first = true;
            }
            //差值为负数
            else {
                last = process(first, -1, i, last, continueNum, delteArray, tmpL, result);
                first = false;
            }
        }
        addToResult(tmpL, continueNum, result);
        return result;
    }

    private int process(boolean first, int flag, int i, int last, int continueNum, DelteData[] delteArray, List<DelteData> tmpL, List<DelteData[]> result) {
        if (first) {
            tmpL.add(delteArray[i]);
            last = flag;
        } else {
            if (last == flag) {
                tmpL.add(delteArray[i]);
            } else {
                addToResult(tmpL, continueNum, result);
                tmpL.clear();
                tmpL.add(delteArray[i]);
                last = flag;
            }
        }
        return last;
    }

    private void addToResult(List<DelteData> tmpL, int continueNum, List<DelteData[]> result) {
        if (!tmpL.isEmpty() && tmpL.size() >= continueNum) {
            result.add(tmpL.toArray(new DelteData[tmpL.size()]));
        }
    }
}
